<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Bangdiem_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
	}
		
	/**
	 * Get all Categories
	 *
	 * @access	public
	 * @return 	object	Categories
	 */
	function get_all_bangdiem($limit, $start)
	{
		$this->db->select('bangdiem.*,giaovien.id as giaovienid, giaovien.tengv, hocvien.id as hocvienid, hocvien.mahv, hocvien.tenhv, monhoc.id as monhocid, monhoc.tenmon, lophoc.id as lophocid, lophoc.malop');
		$this->db->from('bangdiem','giaovien','hocvien','monhoc','lop');
		$this->db->join('giaovien', 'giaovien.id = bangdiem.gv_cham');
		$this->db->join('hocvien', 'hocvien.id = bangdiem.id_hv');
		$this->db->join('monhoc', 'monhoc.id = bangdiem.id_mon');
		$this->db->join('lophoc', 'lophoc.id = bangdiem.id_lop');
		$this->db->where('bangdiem.deleted',0);
		$this->db->order_by('bangdiem.id','desc');
		$this->db->limit($limit, $start);
		$query = $this->db->get();
		return $query->result_array();
	}	
	
	function get_tenkhoa()
	{
		$this->db->select('id, makhoa, tenkhoa');
		$this->db->from('khoa');
		$query = $this->db->get();
		return $query->result_array();
	}
	function get_monhoc()
	{
		$this->db->select('id, mamon, tenmon');
		$this->db->from('monhoc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Get all entries
	 *
	 * @access	public
	 * @return 	object	Entries
	 */
	function add_giaovien()
	{
		$ngaysinh = ($this->input->post('ngaysinh') != '') ? date('Y-m-d', strtotime($this->input->post('ngaysinh'))) : date('Y-m-d');
		$data = array(
			'magv' 			=> $this->input->post('magv'),
			'tengv'			=> $this->input->post('tengv'),
			'email'			=> $this->input->post('email'),
			'didong'		=> $this->input->post('didong'),
			'diachi'		=> $this->input->post('diachi'),
			'ngaysinh'		=> $ngaysinh,
			'gioitinh'		=> $this->input->post('gioitinh'),
			'id_khoa'		=> $this->input->post('makhoa'),
			'cmnd'			=> $this->input->post('cmnd'),
			'created_by'	=> $this->user->id,
			'status'		=> $this->input->post('status'),
			'deleted'		=> 0,
		);
		
		$this->db->set($data);
		$this->db->insert('giaovien');
	}
	
	function add_user()
	{
		$password = $this->input->post('password');
		$hash = sha1(microtime());
		$salt = $this->config->item('auth');
		$salt = $salt['salt'];

		$password = sha1($salt.$hash.$password);
		$data = array(
			'username' 			=> $this->input->post('username'),
			'password'			=> $password,
			'hash'				=> $hash,
			'email'				=> $this->input->post('email'),
			'fullname'			=> $this->input->post('fullname'),
			'mobile'			=> $this->input->post('didong'),
			'FK_group_id'		=> $this->input->post('group'),
			'active'			=> $this->input->post('status'),
		);
		
		$this->db->set($data);
		$this->db->insert('user');
	}
	
	public function check_magv()
	{
		$magv = $this->input->post('magv');
        $query =  "
			SELECT id, magv
			FROM giaovien WHERE magv ='$magv' 
			";
        $result = $this->db->query($query);
        if($result->num_rows() >= 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	public function check_cmnd()
	{
        $this->db->where('cmnd',$this->input->post('cmnd'));
        $query = $this->db->get('giaovien');
        if($query->num_rows() == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	
	public function check_email()
	{
        $this->db->where('email',$this->input->post('email'));
        $query = $this->db->get('giaovien');
        if($query->num_rows() == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	public function get_bangdiem_info($id)
	{
		$this->db->select('*');
		$this->db->from('bangdiem');
		$this->db->where('id', $id);
		$query = $this->db->get();
		return $query->row();
	}
	
	public function edit_giaovien($id)
	{
		$ngaysinh = ($this->input->post('ngaysinh') != '') ? date('Y-m-d', strtotime($this->input->post('ngaysinh'))) : date('Y-m-d');
		
		//$time = mdate('%dd/%mm/%yyyy %H:%m:%s',time());
		$fields = array(
			'magv' 			=> $this->input->post('magv'),
			'tengv'			=> $this->input->post('tengv'),
			'email'			=> $this->input->post('email'),
			'didong'		=> $this->input->post('didong'),
			'diachi'		=> $this->input->post('diachi'),
			'ngaysinh'		=> $ngaysinh,
			'gioitinh'		=> $this->input->post('gioitinh'),
			'id_khoa'		=> $this->input->post('makhoa'),
			'cmnd'			=> $this->input->post('cmnd'),
			'edited_by'		=> $this->user->id,
			'status'		=> $this->input->post('status'),
		);
		
		
		$this->db->set($fields);
		
		$this->db->where('id', $id);
		$this->db->update('giaovien');
	}
	
	function delete_bangdiem($id)
	{
		$fields = array(
			'status' 			=> 0,
			'deleted' 			=> 1,
		);
		$this->db->set($fields);
		$this->db->where('id', $id);
		$this->db->update('bangdiem');
	}
	

	
	public function change_status_bangdiem($id = null,$status = 0)
	{
		$data = array
				(
					'status'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update('bangdiem',$data);		
	}
	
	
	public function search_data($keyword)
	{
		$this->db->select('giaovien.*, khoa.id as khoacid, khoa.makhoa');
		$this->db->from('giaovien','khoa');
		$this->db->join('khoa','khoa.id = giaovien.id_khoa');
		$this->db->where('giaovien.deleted',0);
		$this->db->like('giaovien.magv', $keyword);
		$this->db->or_like('giaovien.cmnd', $keyword);
		$this->db->order_by('giaovien.id', 'DESC');
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}

	public function count_all_bangdiem()
	{
		$this->db->select('id');
		$this->db->from('bangdiem');
		return $this->db->count_all_results();
	}
}
// END giaovien_model.php
/* Location: ./application/models/giaovien_model.php */
